//提交之前把图片上传到服务器
var xjlbid;
var rate;
var shebeibh;
var opener;
var smTime;
//服务端接口路径
var server = Config.upurl + "/api/v2/sbxj/upload";
var AppId = mui.baseApp.AppId; //APPid
var AppKey = mui.baseApp.AppKey; // AppKey
var AppSecret = mui.baseApp.AppSecret; // AppSecret
var Version=localStorage.getItem('version'); // 版本
var timestamp = Date.parse(new Date()); // 时间戳
var OSType; // 系统
var DGUID;
var userxjId;
Version = localStorage.getItem('version'); // 版本
console.log('version'+Version);
OSType = (mui.os.ios) ? "ios" : "android"; // 系统
if(localStorage.getItem("uuid") == null || localStorage.getItem("uuid") == "") {
	DGUID = plus.device.uuid;
	localStorage.setItem("uuid", param.DGUID);
} else {
	DGUID = localStorage.getItem("uuid");
}
mui.plusReady(function() {
	self = plus.webview.currentWebview();
	xjlbid = self.xjlbid;
	 rate = self.rate;
	smTime = self.smTime;
	 shebeibh = self.shebeibh;
	opener = self.opener();
})
// 上传文件
var dataArr = [];
var count = 0;
//	console.log("图片地址。。" + JSON.stringify(saveimg));
function upload(path, i, length) {
//	alert(length);
	var issavePath;
	var wt = plus.nativeUI.showWaiting('加载中.......');
	var task = plus.uploader.createUpload(server + "?AppId=" + AppId + "&AppKey=" + AppKey + "&AppSecret=" + AppSecret + "&version=" + Version + "&timestamp=" + timestamp + "&OSType=" + OSType + "&DGUID=" + DGUID, {
			method: "POST"
		},
		function(t, status) { //上传完成
			console.log(server + "?AppId=" + AppId + "&AppKey=" + AppKey + "&AppSecret=" + AppSecret + "&version=" + Version + "&timestamp=" + timestamp + "&OSType=" + OSType + "&DGUID=" + DGUID)
			if(status == 200) {
				console.log("上传成功：" + t.responseText);
				if(JSON.parse(t.responseText).status == 200) {
					var date = JSON.parse(t.responseText);
					issavePath = date.data.savePath;
					var up_img = {
						i: i,
						issavePath: issavePath
					}
					count++;
					console.log('上传路径：' + issavePath);

					dataArr.push(up_img);
					localStorage.setItem('saveimg1', JSON.stringify(dataArr));
					console.log("图片地址1。。" + JSON.stringify(dataArr));
					wt.close();
//										alert(count);
					if(count == length) {
						tijiao();
					}
				} else if(JSON.parse(t.responseText).status == 400) {
					mui.toast(JSON.parse(t.responseText).message);
					wt.close();
					return;
				} else {
					wt.close();
					return;
				}
				wt.close(); //关闭等待提示按钮
			} else {
				console.log("上传失败：" + status);
				wt.close(); //关闭等待提示按钮
				mui.toast("上传失败");
			}
		}

	);
	//			//添加其他参数
	task.addData("file", path);
	task.addFile(path, {
		key: path
	});
	task.start();
	//	console.log('status3'+issavePath);
}
var isnormal=0;
function tijiao() {
	var saveimg1 = JSON.parse(localStorage.getItem('saveimg1'));
	console.log("有网图片地址。。" + JSON.stringify(saveimg1));
	var list = $('#yulan .mui-card');
	var arr = [];
	for(var i = 0; i < list.length; i++) {
		var obj = {};
		obj.desc = $(list[i]).find('.beizhu').text();
		obj.id = $(list[i]).attr('data-id');
		//						alert(obj.id);
		if(obj.desc!=""){
			isnormal=1;
		}
		obj.state = $(list[i]).find('span.state').text();
		var attach="";
		if(saveimg1==null){
			
		}else{
			for (var m=0;m<saveimg1.length;m++){
				if(saveimg1[m].i === i) {
					attach = saveimg1[m].issavePath;
				}
			}
		}
		obj.attach = attach;
		arr.push(obj);
	}
	var jcxArray = JSON.stringify(arr);
	
	console.log('提交本地：'+JSON.stringify(arr));
	console.log(jcxArray);
		
//	document.querySelector('#submit').addEventListener('tap', function() {
//		if (plus.networkinfo.getCurrentType()  ==  plus.networkinfo.CONNECTION_NONE)  {
//			mui.toast('当前无网络不能提交')
//		} else {

			var btnArr = ['确认', '取消'];
			mui.confirm('确认提交吗?', '操作提示', btnArr, function(e) {
				if(e.index == 0) {
					localStorage.removeItem('saveimg1');
					plus.nativeUI.showWaiting('提交中...');
					mui.baseApp.httpRequst('/api/v2/sbxj/submitUserXj', {
						memberId: localStorage.getItem('userid'),
						rate: rate,
						scancode: shebeibh,
						inspect_time: smTime,
						isnormal: isnormal,
						jcxArray: jcxArray,
						userxjId: userxjId
					}, function(data) {
						plus.nativeUI.closeWaiting();
						console.log('111111' +JSON.stringify( data));
						if(data.status == "200") {
							mui.toast(data.message);
							draglistremove();
							try{
							plus.webview.getWebviewById("new/record_draft.html").evalJS('local()');
							plus.webview.getWebviewById("record_sub.html").evalJS('pulldown()');
							}catch(err){}
							var page = plus.webview.getWebviewById("jiankong.html");
							mui.fire(page, 'home');
							console.log('1111');
							plus.webview.currentWebview().close();
							
						} else if(data.status == "400") {
							mui.toast(data.message);
						} else if(data.status == "409") {
							mui.toast(data.message);
						}

					})
				}

			});
//		}

//	});

}
//新建进入提交成功后判断草稿箱中是否有，有的话删除
function draglistremove() {
	var save = JSON.parse(localStorage.getItem("savexcxj"));

	for(var m = 0; m < save.length; m++) {
		if(save[m].userxjId == xjlbid) {
			save.splice(m, 1);
			console.log("提交后的草稿箱数组1068。。" + JSON.stringify(save))
			localStorage.setItem("savexcxj", JSON.stringify(save));
			plus.webview.getWebviewById("new/record_draft.html").evalJS("local()");
		}
	}
}